#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
裸眼3D视频转换器 - 样式文件

本文件包含应用程序的样式设置，用于统一设置所有控件的风格样式，
使其与窗口的毛玻璃效果协调一致。
"""

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QTextEdit, QComboBox, QSpinBox, QCheckBox, QLabel, QGroupBox, QTabWidget, QSplitter, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtCore import Qt


# 主题颜色
PRIMARY_COLOR = "#3498db"  # 主色调
SECONDARY_COLOR = "#2980b9"  # 次要色调
BACKGROUND_COLOR = "rgba(52, 73, 94, 180)"  # 深色半透明背景
TEXT_COLOR = "#FFFFFF"  # 文字颜色（白色）
BORDER_COLOR = "#87CEEB"  # 边框颜色（浅蓝色）
HOVER_COLOR = "#2ecc71"  # 悬停颜色
DISABLED_COLOR = "#bdc3c7"  # 禁用状态颜色

# 基础样式表
BASE_STYLESHEET = f"""
* {{
    font-family: 'Microsoft YaHei', 'SimHei', sans-serif;
    font-size: 12px;
    color: {TEXT_COLOR};
}}

QWidget {{
    background-color: transparent;
}}

QGroupBox, QWidget#content_area > QWidget, QTabWidget::pane {{
    border: 1px solid {BORDER_COLOR};
    border-radius: 5px;
    margin-top: 10px;
    background-color: {BACKGROUND_COLOR};
}}

QGroupBox::title {{
    subcontrol-origin: margin;
    subcontrol-position: top center;
    padding: 0 5px;
    color: {PRIMARY_COLOR};
    font-weight: bold;
}}

QPushButton {{
    background-color: {PRIMARY_COLOR};
    color: white;
    border: none;
    border-radius: 4px;
    padding: 5px 15px;
    min-height: 25px;
}}

QPushButton:hover {{
    background-color: {HOVER_COLOR};
}}

QPushButton:pressed {{
    background-color: {SECONDARY_COLOR};
}}

QPushButton:disabled {{
    background-color: {DISABLED_COLOR};
    color: #7f8c8d;
}}

QPushButton:checked {{
    background-color: {SECONDARY_COLOR};
    color: white;
}}

QLineEdit, QComboBox, QSpinBox, QCheckBox::indicator {{
    border: 1px solid {BORDER_COLOR};
    border-radius: 4px;
    padding: 3px 5px;
    background-color: rgba(255, 255, 255, 200);
    selection-background-color: {PRIMARY_COLOR};
}}

QLineEdit:focus {{
    border: 1px solid {PRIMARY_COLOR};
}}

QLineEdit:disabled {{
    background-color: rgba(200, 200, 200, 150);
    color: #7f8c8d;
}}

QTextEdit {{
    border: 1px solid {BORDER_COLOR};
    border-radius: 4px;
    padding: 3px 5px;
    background-color: rgba(255, 255, 255, 200);
    selection-background-color: {PRIMARY_COLOR};
}}

QTextEdit:focus {{
    border: 1px solid {PRIMARY_COLOR};
}}

QComboBox {{
    border: 1px solid {BORDER_COLOR};
    border-radius: 4px;
    padding: 3px 5px;
    background-color: rgba(255, 255, 255, 200);
    min-height: 25px;
}}

QComboBox::drop-down {{
    subcontrol-origin: padding;
    subcontrol-position: right center;
    width: 20px;
    border-left: 1px solid {BORDER_COLOR};
}}

QComboBox::down-arrow {{
    width: 10px;
    height: 10px;
    image: url(resources/dropdown_arrow.svg);
}}

QComboBox QAbstractItemView {{
    border: 1px solid {BORDER_COLOR};
    border-radius: 0px;
    background-color: white;
    selection-background-color: {PRIMARY_COLOR};
    selection-color: white;
}}

QSpinBox {{
    border: 1px solid {BORDER_COLOR};
    border-radius: 4px;
    padding: 3px 5px;
    background-color: rgba(255, 255, 255, 200);
    min-height: 25px;
}}

QSpinBox::up-button, QSpinBox::down-button {{
    border: none;
    width: 16px;
    background-color: {PRIMARY_COLOR};
}}

QSpinBox::up-button:hover, QSpinBox::down-button:hover {{
    background-color: {HOVER_COLOR};
}}

QSpinBox::up-arrow {{
    width: 10px;
    height: 10px;
    image: url(resources/up_arrow.svg);
}}

QSpinBox::down-arrow {{
    width: 10px;
    height: 10px;
    image: url(resources/down_arrow.svg);
}}

QCheckBox {{
    spacing: 5px;
}}

QCheckBox::indicator {{
    width: 15px;
    height: 15px;
    border: 1px solid {BORDER_COLOR};
    border-radius: 3px;
    background-color: rgba(255, 255, 255, 200);
}}

QCheckBox::indicator:checked {{
    background-color: {PRIMARY_COLOR};
    image: url(resources/check.svg);
}}

QCheckBox::indicator:hover {{
    border: 1px solid {HOVER_COLOR};
}}

QLabel {{
    color: {TEXT_COLOR};
    min-width: 80px;
    margin-right: 10px;
}}

QSplitter::handle {{
    background-color: {BORDER_COLOR};
}}

QSplitter::handle:horizontal {{
    width: 1px;
}}

QSplitter::handle:vertical {{
    height: 1px;
}}

QTabWidget::pane {{
    border: 1px solid {BORDER_COLOR};
    border-radius: 5px;
    background-color: {BACKGROUND_COLOR};
}}

QTabBar::tab {{
    background-color: rgba(200, 200, 200, 150);
    border: 1px solid {BORDER_COLOR};
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    padding: 5px 10px;
    margin-right: 2px;
}}

QTabBar::tab:selected {{
    background-color: {PRIMARY_COLOR};
    color: white;
}}

QTabBar::tab:hover {{
    background-color: {HOVER_COLOR};
    color: white;
}}
"""


def apply_stylesheet(app):
    """
    应用样式表到整个应用程序
    
    Args:
        app: QApplication实例
    """
    app.setStyleSheet(BASE_STYLESHEET)


def style_left_panel(panel):
    """
    设置左侧面板样式
    
    Args:
        panel: 左侧面板QWidget实例
    """
    panel_style = f"""
    QWidget {{
        background-color: rgba(52, 73, 94, 180);
        border-radius: 5px;
    }}
    
    QPushButton {{
        text-align: left;
        padding: 10px 15px;
        border-radius: 0px;
        background-color: transparent;
        color: white;
        font-weight: bold;
    }}
    
    QPushButton:hover {{
        background-color: rgba(46, 204, 113, 150);
    }}
    
    QPushButton:checked {{
        background-color: rgba(41, 128, 185, 200);
        border-left: 3px solid {HOVER_COLOR};
    }}
    """
    
    panel.setStyleSheet(panel_style)


def style_log_panel(panel):
    """
    设置日志面板样式
    
    Args:
        panel: 日志面板QWidget实例
    """
    log_style = f"""
    QWidget {{
        background-color: rgba(52, 73, 94, 180);
        border-radius: 5px;
    }}
    
    QLabel {{
        font-weight: bold;
        color: white;
        font-size: 14px;
    }}
    
    QTextEdit {{
        background-color: rgba(44, 62, 80, 180);
        border: 1px solid {BORDER_COLOR};
        border-radius: 5px;
        font-family: 'Consolas', 'Courier New', monospace;
        color: white;
    }}
    """
    
    panel.setStyleSheet(log_style)


# 在create_svg_resources函数中添加裁切图标
def create_svg_resources():
    """
    创建SVG资源文件
    """
    import os
    
    # 确保resources目录存在
    resources_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources")
    if not os.path.exists(resources_dir):
        os.makedirs(resources_dir)
    
    # 下拉箭头SVG
    dropdown_arrow = '''
    <svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 10 10">
        <path d="M1,3 L5,7 L9,3" stroke="#3498db" stroke-width="2" fill="none"/>
    </svg>
    '''
    
    # 上箭头SVG
    up_arrow = '''
    <svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 10 10">
        <path d="M1,7 L5,3 L9,7" stroke="white" stroke-width="2" fill="none"/>
    </svg>
    '''
    
    # 下箭头SVG
    down_arrow = '''
    <svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 10 10">
        <path d="M1,3 L5,7 L9,3" stroke="white" stroke-width="2" fill="none"/>
    </svg>
    '''
    
    # 勾选SVG
    check = '''
    <svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 15 15">
        <path d="M3,7 L6,10 L12,4" stroke="white" stroke-width="2" fill="none"/>
    </svg>
    '''
    
    # 写入SVG文件
    with open(os.path.join(resources_dir, "dropdown_arrow.svg"), "w") as f:
        f.write(dropdown_arrow)
    
    with open(os.path.join(resources_dir, "up_arrow.svg"), "w") as f:
        f.write(up_arrow)
    
    with open(os.path.join(resources_dir, "down_arrow.svg"), "w") as f:
        f.write(down_arrow)
    
    with open(os.path.join(resources_dir, "check.svg"), "w") as f:
        f.write(check)


def style_content_area(tab_widget):
    """
    设置内容区域整体样式
    
    Args:
        tab_widget: QTabWidget实例
    """
    content_area_style = f"""
    QTabWidget::pane {{
        border: none;
        background-color: transparent;
    }}
    
    QWidget#content_area {{
        background-color: transparent;
    }}
    """
    
    tab_widget.setStyleSheet(content_area_style)
    tab_widget.setObjectName("content_area")
    
    # 设置透明背景
    set_widget_palette(tab_widget)


def style_content_controls(widget):
    """
    设置中间内容区域的控件样式，模拟毛玻璃效果
    
    Args:
        widget: 中间内容区域的QWidget实例
    """
    content_controls_style = f"""
    QWidget {{
        background-color: transparent;
    }}
    
    QGroupBox {{
        background-color: rgba(52, 73, 94, 150);
        border: 1px solid white;
        border-radius: 5px;
        margin-top: 15px;
        padding-top: 15px;
        padding-bottom: 10px;
    }}
    
    QGroupBox::title {{
        subcontrol-origin: margin;
        subcontrol-position: top center;
        padding: 0 10px;
        background-color: rgba(52, 73, 94, 200);
        color: white;
        font-weight: bold;
        font-size: 13px;
    }}
    
    QLabel {{
        color: white;
        background-color: transparent;
        font-size: 12px;
        min-width: 80px;
        margin-right: 5px;
    }}
    
    QComboBox {{
        border: 1px solid white;
        border-radius: 4px;
        padding: 3px 5px;
        background-color: rgba(255, 255, 255, 150);
        color: white;
        selection-background-color: {PRIMARY_COLOR};
        min-width: 150px;
    }}
    
    QComboBox QAbstractItemView {{
        border: 1px solid white;
        color: white;
        background-color: rgba(52, 73, 94, 220);
        selection-background-color: {PRIMARY_COLOR};
    }}
    
    QPushButton {{
        border: 1px solid white;
        background-color: rgba(52, 128, 185, 200);
        color: white;
        padding: 5px 15px;
    }}
    
    QPushButton:hover {{
        background-color: rgba(46, 204, 113, 200);
    }}
    
    QCheckBox {{
        color: white;
    }}
    
    QCheckBox::indicator {{
        border: 1px solid white;
    }}
    
    QProgressBar {{
        border: 1px solid white;
        border-radius: 4px;
        background-color: rgba(255, 255, 255, 100);
        text-align: center;
        color: white;
    }}
    
    QProgressBar::chunk {{
        background-color: {PRIMARY_COLOR};
        width: 10px;
        margin: 0.5px;
    }}
    """
    
    widget.setStyleSheet(content_controls_style)
    widget.setObjectName("content_page")
    
    # 设置透明背景
    set_widget_palette(widget)
    
    # 调整布局间距，使控件更紧凑
    for layout in widget.findChildren(QVBoxLayout) + widget.findChildren(QHBoxLayout):
        layout.setSpacing(6)  # 减小控件之间的间距
        layout.setContentsMargins(10, 10, 10, 10)  # 设置布局边距


def set_widget_palette(widget):
    """
    设置控件的调色板，使其背景透明或半透明
    
    Args:
        widget: QWidget实例
    """
    palette = widget.palette()
    
    # 根据控件类型设置不同的透明度
    if isinstance(widget, QGroupBox):
        bg_color = QColor(52, 73, 94, 150)  # 半透明深蓝色
    elif isinstance(widget, QTabWidget):
        bg_color = QColor(0, 0, 0, 0)  # 完全透明
    else:
        bg_color = QColor(0, 0, 0, 0)  # 完全透明
    
    palette.setColor(QPalette.Window, bg_color)
    widget.setPalette(palette)
    widget.setAttribute(Qt.WA_TranslucentBackground)

